package com.atshiyou.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
public class UploadController {
    @RequestMapping("upload")
    public String upload(MultipartFile myfile, HttpServletRequest request){
        //处理上传的文件内容
        //将上传的文件夹转化成服务器上的路径
        String realPath = request.getRealPath("/uploadImage");
        System.out.println("realPath="+realPath);
        //得到上传的文件名
        String originalFilename = myfile.getOriginalFilename();
        System.out.println("文件的名称:"+originalFilename);
        //准备上传
        try {
            myfile.transferTo(new File(realPath+"/"+originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        request.getSession().setAttribute("filename",originalFilename);
        return "uploadsuccess";
    }
}
